﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;

namespace DC.Views
{
    public partial class Form_InsertTemplate : Skin_DevExpress
    {
        public Form_InsertTemplate()
        {
            InitializeComponent();
        }

        private void Form_InsertTemplate_Load(object sender, EventArgs e)
        {
            LoadTemplate();
        }
        Utils.Database DB = Utils.Global.GlobalDB;
        private void LoadTemplate()
        {
            string sql = "select Template_ID, Template_Type from Template order by Template_ID DESC ";
            DataView TemplateView = DB.GetDs(sql).Tables[0].DefaultView;
            listBox1.DataSource = TemplateView;
            listBox1.ValueMember = "Template_ID";
            listBox1.DisplayMember = "Template_Type";
        }
        public void searchTemplate()
        {
            string sql = "select Template_ID,Template_Type from Template where 1=1 ";
            if (textBox1.Text != string.Empty)
            {
                sql += "and Template_Type like'%" + textBox1.Text + "%'";
            }
            DataView TemplateView = DB.GetDs(sql).Tables[0].DefaultView;
            listBox1.DataSource = TemplateView;
            listBox1.ValueMember = "Template_ID";
            listBox1.DisplayMember = "Template_Type";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            searchTemplate();
        }

        public string TemplateID = string.Empty;
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count == 0)
            {
                return;
            }
            DialogResult drTemp = MessageBox.Show("确定选择该模版！", "确认框", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (drTemp == DialogResult.Yes)
            {
                TemplateID = listBox1.SelectedValue.ToString();
                Close();
                return;
            }
        }

        private void 删除模板ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItems.Count == 0)
            {
                return;
            }
            DialogResult a = MessageBox.Show("确定删除？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (a == DialogResult.Yes)
            {
                DB.Execute("delete  from Template where Template_ID=" + listBox1.SelectedValue);
            }
            LoadTemplate();
        }

        private void 重命名ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Utils.InputBox box = new Utils.InputBox();
            box.Title = "请输入";
            box.MessageText = "重新病历模板名称：";
            box.ShowDialog();
            if (box.ButtonClick != 1)
            {
                return;
            }
            string name = box.Content;
            if (name == string.Empty)
            {
                MessageBox.Show("请重新输入保存的病例模板名称！", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DB.Execute("update Template set Template_Type='" + name + "' WHERE Template_ID=" + listBox1.SelectedValue);
            LoadTemplate();
        }
    }
}
